const express = require('express');
const router = express.Router();
const crypto = require('crypto');
//生成一个随机盐
const salt = crypto.randomBytes(16);



function userRouteModule(db) {

    //注册接口
    router.post('/register', async (req, res) => {
        const { username, password } = req.body;
        //创建哈希
        const hash = crypto.createHmac('sha256', salt).update(password).digest('hex');
        console.log(hash, "hash");
        console.log(salt, "salt")
        const sql = 'INSERT INTO user (username,password,salt) VALUES (?,?,?)'

        try {
            const [result] = await db.query(sql, [username, hash, salt]);
            console.log(result, "result")
            res.status(200).send({ code: 200, msg: '注册成功' });

        } catch (error) {
            console.log(error, "错误");
        }

        res.send('register')
    })


    // 登录接口
    router.post('/login', (req, res) => {
        res.send('login')
    })



    return router;
}





module.exports = userRouteModule;